package com.h3bpm.web.controller;

import OThinker.Common.Data.Database.Parameter;
import OThinker.Common.Data.DbType;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.RestfulApi.cloudpivot.ResponseResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.web.service.ExpensesOrReceiptsService;
import data.DataException;
import data.DataRow;
import data.DataRowCollection;
import data.DataTable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 收入或支出
 *
 * @author leo
 */
@RequestMapping("/api/expensesOrReceipts")
@RestController
public class ExpensesOrReceiptsController extends ControllerBase {
    private static final Logger logger = LoggerFactory.getLogger(ExpensesOrReceiptsController.class);

    private static final String credentialMethod ="凭证结算";

    @Autowired
    ExpensesOrReceiptsService expensesOrReceiptsService;

    /**
     *
     * @param contractNumber
     * @param contractName
     * @param orderType
     * @param settlementMethod
     * @param actualPayment
     * @param performanceTime
     * @param objectId
     * @return
     */
    @RequestMapping("/setOrderPayment")
    public ResponseResult setOrderPayment(String contractNumber, String contractName,String orderType,String settlementMethod, BigDecimal actualPayment, String performanceTime,String objectId) {
        logger.info("参数-1  contractNumber:    " + contractNumber);
        logger.info("参数-2  contractName:      " + contractName);
        logger.info("参数-3  orderType:         " + orderType);
        logger.info("参数-4  settlementMethod:  " + settlementMethod);
        logger.info("参数-5  actualPayment:     " + actualPayment);
        logger.info("参数-6  performanceTime:   " + performanceTime);
        logger.info("参数-7  objectId:          " + objectId);
        ResponseResult rst = new ResponseResult(-1L, "", null);
        try {
            if (credentialMethod.equals(settlementMethod)) {
                logger.info("开始执行凭证结算");
                expensesOrReceiptsService.setMasterDataByCredential(contractNumber, actualPayment, performanceTime);
            } else {
                logger.info("开始执行订单结算");
                expensesOrReceiptsService.setOrderInfo(contractNumber, contractName, orderType,objectId);
            }

        } catch (Exception e) {
            rst.setErrmsg(e.getMessage());
            logger.info(e.getMessage());
            return rst;
        }
        rst.setErrmsg("执行成功");
        rst.setErrcode(0L);
        return rst;
    }
    @RequestMapping("/removeOrderPayment")
    public ResponseResult removeOrderPayment(String contractNumber) {
        ResponseResult rst = new ResponseResult(-1L, "", null);
        if (StringUtils.isBlank(contractNumber)){
            rst.setErrmsg("参数不正确");
            return rst;
        }
        Boolean aBoolean = expensesOrReceiptsService.removeOrderInfo(contractNumber);
        if (aBoolean){
            rst.setErrmsg("成功");
            rst.setErrcode(0L);
            return rst;
        }
        rst.setErrmsg("失败");
        return rst;
    }




    @Override
    public String getFunctionCode() {
        return null;
    }
}
